<?php

class UserApi
{
    public static $autoLoadAttributes=array('id','name');
 
    public static function loadById($id,$attributes='*')
    {
        if(is_array($attributes)){
            $attributes=array_unique(array_merge($attributes,self::$autoLoadAttributes));
        }
        return self::loadObject(array(
            'condition'=>'name=:name',
            'params'=>array(':name'=>$name),
            'select'=>$attributes,
        ));    
    }
    
    public static function loadByName($name,$attributes='*')
    {
        if(is_array($attributes)){
            $attributes=array_unique(array_merge($attributes,self::$autoLoadAttributes));
        }
        return self::loadObject(array(
            'condition'=>'name=:name',
            'params'=>array(':name'=>$name),
            'select'=>$attributes,
        ));
    }
    
    public static function loadByEmail($email,$attributes='*')
    {
        if(is_array($attributes)){
            $attributes=array_unique(array_merge($attributes,self::$autoLoadAttributes));
        }
        return self::load(array(
            'condition'=>'email=:email', 
            'params'=>array(':email'=>$email),
            'select'=>$attributes,
        ));
    }

    public static function load($criteria=array())
    {
        $criteria['limit']=1;                                 
        return array_shift(User::loadObjects($criteria));
    }

    public static function save(&$user, $attributes=null)
    {
        if(is_array($attributes))
            $user->_saveAttributes=array_flip($attributes);
        else
            $user->_saveAttributes=$attributes;

        if($object->getIsNewRecord()){
            $ret=self::createObject($object, $attributes);
        }
        else {
            $ret=$user->save(true, $attributes);
        }

        unset($object->_saveAttributes);
        return $ret;
    }

    public static function delete(&$user)
    {
        return $object->delete();
    }
    
    protected static function createObject(&$object, $attributes=null)
    {
        $transaction=Yii::app()->db->beginTransaction();
        try {
            if(!$object->save(true, $attributes))
                return false;

            $kitchen=new Kitchen;
            $kitchen->id=$object->id;
            $kitchen->userName=$object->name;
            $kitchen->title=sprintf("%s的厨房", $object->name);

            if(!$kitchen->save()) 
                throw new CException('create kitchen failed');
                
            $transaction->commit();
            return true;
        }
        catch(Exception $e){
            $transaction->rollBack();
            if(strstr($e->getMessage(),'1062 Duplicate entry')===false) {
                throw $e;
            }
            if(strstr($e->getMessage(),'for key 2')!==false) {
                $object->addError('name', $object->getAttributeLabel('name').$object->name.'已存在');
            }
            elseif(strstr($e->getMessage(),'for key 3')!==false) {
                $object->addError('email', $object->getAttributeLabel('email').$object->email.'已存在');
            }
            return false;
        }
    }    
}